package com.example.tourapp.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.example.tourapp.R;

import java.util.List;

public class Viewpager_Adapetr extends PagerAdapter {
    private Context context;
    private int [] v_imge;
    private String [] v_content;

    public Viewpager_Adapetr(Context context, int[] v_imge, String[] v_content) {
        this.context = context;
        this.v_imge = v_imge;
        this.v_content = v_content;
    }

    @Override
    public int getCount() {
        return v_imge.length;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        //super.destroyItem(container, position, object);
        container.removeView((View) object);
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.mode_tour, container, false);
        ImageView tour_img = view.findViewById(R.id.tour_img);
        TextView tour_content=view.findViewById(R.id.tour_content);
        tour_img.setImageResource(v_imge[position]);
        tour_content.setText(v_content[position]);
        //添加到布局管理器中，方便destroyItem方法中销毁
        container.addView(view);
        return view;
    }
}
